package org.vectrics.recruiter.struts.admin;

import java.util.List;

import javax.servlet.jsp.JspException;

import org.apache.struts.action.ActionMessages;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobTerm;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;

/**
 * @author pgoette
 * Action encargado de borrar el Term
 */
public class DeleteJobTermAction extends VectricsStrutsAction {


	@Override
	protected String doExecute(VectricsActionData actionData)
			throws JspException {
		
		String forward = "success";
		Long idTerm = actionData.getRequest().getLongParameter("termId");
		
		if (idTerm != null) {
			JobTerm term = RecruitServiceLocator.getJobProvider().findJobTerm(idTerm);
			// Recupero los job
			List<Job> jobs = RecruitServiceLocator.getJobProvider().findJobs();
			for (Job job : jobs){
				// Si un job usa el term genero un error y salgo.
				if (job.getTermCode().equalsIgnoreCase(term.getCode())) {
					actionData.addError(ActionMessages.GLOBAL_MESSAGE, "error.delete.job.term");
					return forward;
				}
			}
			// Borro el term
			RecruitServiceLocator.getJobProvider().deleteJobTerm(idTerm);
		} 
		
		return forward;
	}

}
